{
 "metadata": {
  "name": "",
  "signature": "sha256:6429df991452a125759470f6954c195e7c6c93718615e4b1e6739ec3e5821090"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#\u0413\u0440\u0443\u0437\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435\n",
      "\n",
      "q=open(\"car.data\",\"r\")\n",
      "all_data = [ a.strip().split(\",\") for a in q]\n",
      "#plus = [a for a in train if a[6]==\"acc\" or a[6]==\"good\" or a[6]==\"v-good\"]\n",
      "#minus = [a for a in train if a[6]==\"unacc\"]\n",
      "\n",
      "#print(len(all_data))\n",
      "\n",
      "#print('len all data:', len(all_data))\n",
      "\n",
      "#\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430\n",
      "\n",
      "\"\"\"   \n",
      "index    name         values                   replacement values\n",
      "0        buying       vhigh, high, med, low   11, 10, 01, 00\n",
      "1        maint        vhigh, high, med, low   11, 10, 01, 00\n",
      "2        doors        2, 3, 4, 5-more          00, 01, 10, 11\n",
      "3        persons      2, 4, more               00, 10, 11\n",
      "4        lug_boot     small, med, big          01, 10, 11\n",
      "5        safety       low, med, high           01, 10, 11\n",
      "\"\"\"\n",
      "\n",
      "data = [['','','','','','',''] for x in range(len(all_data))]\n",
      "#print(len(data))\n",
      "\n",
      "replacement_dict = {\n",
      " \"vhigh\": '11',\n",
      " \"high\": '10',\n",
      " \"med\": '01',\n",
      " \"low\": '00',\n",
      " \"2\": '00',\n",
      " \"3\": '01',\n",
      " \"4\": '10',\n",
      " \"5more\": '11',\n",
      " \"more\": '11',\n",
      " \"small\": '01',\n",
      " \"low\": '01',\n",
      " \"med\": '10',\n",
      " \"big\": '11',\n",
      " \"high\": '11',\n",
      " \"acc\": '1',\n",
      " \"good\": '1',\n",
      " \"vgood\": '1',\n",
      " \"unacc\": '0',\n",
      "}\n",
      "\n",
      "\n",
      "for i in range(0, len(data)):\n",
      "    for j in range(0, 7):\n",
      "        data[i][j] = replacement_dict[all_data[i][j]]\n",
      "\n",
      "#print(train[0])\n",
      "#print(data[0])\n",
      "\n",
      "for i in range(0,len(data)):\n",
      "    data[i] = ''.join(data[i])\n",
      "    \n",
      "#print(len(data[0]))\n",
      "\n",
      "#\u0414\u0435\u043b\u0430\u0435\u043c \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u0443\u044e \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \n",
      "\n",
      "plus = [a for a in data if a[-1]==\"1\"] #\u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\n",
      "minus = [a for a in data if a[-1]==\"0\"] #\u043f\u043e\u043b\u043d\u044b\u0439 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\n",
      "\n",
      "from random import shuffle\n",
      "\n",
      "plus_shuffle = []\n",
      "plus_shuffle = plus\n",
      "shuffle(plus_shuffle)\n",
      "#print(plus_shuffle[0])\n",
      "minus_shuffle = []\n",
      "minus_shuffle = minus\n",
      "shuffle(minus_shuffle)\n",
      "#print(type(int(minus_shuffle[0],2)))\n",
      "\n",
      "train = plus_shuffle[:100]+minus_shuffle[:100]\n",
      "test = plus_shuffle[101:201]+minus_shuffle[101:201]\n",
      "remain = plus_shuffle[200:]+minus_shuffle[200:]\n",
      "#print(type(int(train[0],2)))\n",
      "#print(len(test))\n",
      "#print(len(remain))\n",
      "\n",
      "#print(plus)\n",
      "#print(minus)\n",
      "\n",
      "#\u0411\u0438\u043d\u0430\u0440\u0438\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u0442\u0440\u0435\u0439\u043d \u0438 \u0442\u0435\u0441\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b\n",
      "train_bin = []\n",
      "test_bin = []\n",
      "for i in range(0,len(train)):\n",
      "    train_bin.append(int(train[i][:-1],2))\n",
      "    test_bin.append(int(test[i][:-1],2))\n",
      "    \n",
      "plus_bin = train_bin[:100]\n",
      "minus_bin = train_bin[100:]\n",
      "\n",
      "#print(len(plus_bin))\n",
      "#print(len(minus_bin))\n",
      "\n",
      "#\u0421\u043f\u0438\u0441\u043e\u043a \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0437\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\n",
      "vote_list = []\n",
      "\n",
      "#\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0444\u0430\u043b\u044c\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0433\u0438\u043f\u043e\u0442\u0435\u0437\n",
      "def falsify(mask_bin, context_bin):\n",
      "    for i in range(0,len(context_bin)-1):\n",
      "        if mask_bin & context_bin[i] == mask_bin:\n",
      "            return 0\n",
      "        else:\n",
      "            return 1\n",
      "\n",
      "#\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430\n",
      "def get_vote(to_detect, plus_bin, minus_bin):\n",
      "    vote_count = 0\n",
      "    for i in range(0,len(plus_bin)-1):\n",
      "        mask = to_detect & plus_bin[i]\n",
      "        vote_count += falsify(mask, minus_bin)\n",
      "    for i in range(0,len(plus_bin)-1):\n",
      "        mask = to_detect & minus_bin[i]\n",
      "        vote_count -= falsify(mask, plus_bin)\n",
      "    return vote_count\n",
      "\n",
      "#\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0437\u0430 \u043d\u0435\u0433\u043e\n",
      "for i in range(0, len(train_bin)):\n",
      "    to_detect = train_bin[i]\n",
      "    vote = get_vote(to_detect, plus_bin, minus_bin)\n",
      "    vote_list.append(vote)\n",
      "    #print('vote for object #', i, '('+train[i]+')', vote)\n",
      "    \n",
      "cv_res = {\n",
      " \"11\": 0,  #true positive\n",
      " \"10\": 0,  #false negative\n",
      " \"01\": 0,  #false positive\n",
      " \"00\": 0,  #true negative\n",
      " \"NaN\": 0, #unclassified\n",
      "}\n",
      "\n",
      "#\u0421\u0447\u0438\u0442\u0430\u0435\u043c \u043d\u0430 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0447\u0430\u0441\u0442\u0438 (\u0432\u0441\u0451-train-test), \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f\n",
      "#\u041f\u043b\u0430\u043d\u043a\u0443 -20 \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u044d\u043c\u043f\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u0438. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0438\u0436\u0435 \u0438\u043b\u0438 \u0432\u044b\u0448\u0435, \u0434\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0445\u0443\u0436\u0435. \n",
      "\n",
      "for i in remain:\n",
      "    det_str = i[:-1]\n",
      "    to_detect = int(''.join(det_str))\n",
      "    vote = get_vote(to_detect, plus_bin, minus_bin)\n",
      "    if vote >= -20:\n",
      "        decision = '1'\n",
      "    elif vote < -20:\n",
      "        decision = '0'\n",
      "    else:\n",
      "        decision = 'NaN'\n",
      "    if i[0] == '1' and decision == '1':\n",
      "        cv_res['11'] +=1\n",
      "    elif i[0] == '1' and decision == '0':\n",
      "        cv_res['10'] +=1\n",
      "    elif i[0] == '0' and decision == '1':\n",
      "        cv_res['01'] +=1\n",
      "    elif i[0] == '0' and decision == '0':\n",
      "        cv_res['00'] +=1\n",
      "    else:\n",
      "        cv_res['NaN'] +=1\n",
      "\n",
      "print('accuracy: ', (cv_res['11']+cv_res['00'])/(cv_res['11']+cv_res['00']+cv_res['01']+cv_res['10']+cv_res['NaN']))\n",
      "print('true-positive: ', cv_res['11'], 'true-negative: ', cv_res['00'])\n",
      "print('false-negative: ', cv_res['10'], 'false-positive: ', cv_res['01'])\n",
      "print('uncertain:', cv_res['NaN'])\n",
      "\n",
      "#\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432\u044b\u0448\u0435\u043b \u043d\u0435 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e (\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c 65-75%, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0440\u0435\u0439\u043d\u0430). \n",
      "#\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434.\n",
      "#\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u044b. \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "accuracy:  0.7356927710843374\n",
        "true-positive:  968 true-negative:  9\n",
        "false-negative:  29 false-positive:  322\n",
        "uncertain: 0\n"
       ]
      }
     ],
     "prompt_number": 208
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 186
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}